package com.you.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.you.common.QueryPageParam;
import com.you.common.Result;
import com.you.entity.User;
import com.you.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 友友
 * @since 2022-10-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/list")
    public List<User> list() {
        return userService.list(null);
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user)? Result.suc():Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public Result update(@RequestBody User user) {
        return userService.updateById(user)? Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam Integer id) {
        return userService.removeById(id)? Result.suc():Result.fail();
    }

    //新增或修改
    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }

    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id) {
        return userService.removeById(id);
    }

    //模糊查询
    @PostMapping("/mHList")
    public List<User> mHList(@RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(user.getName())){
            wrapper.like(User::getName, user.getName());
        }

        return userService.list(wrapper);
    }

    //分页
    @PostMapping("/listPage")
    public List<User> listPage(@RequestBody QueryPageParam pageParam) {

        System.err.println("size = " + pageParam.getPageSize());
        System.err.println("num = " + pageParam.getPageNum());

        HashMap param = pageParam.getParam();
        String name = (String)param.get("name");
//        System.err.println("name = " + param.get("name"));
//        System.err.println("no = " + param.get("no"));

//        Page<User> page = new Page<>(1,2);
        Page<User> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(name)){
            wrapper.like(User::getName, name);
        }

        IPage result = userService.page(page, wrapper);
        System.err.println(result.getTotal());

        return result.getRecords();

    }

    //自定义分页
    @PostMapping("/diyListPage")
    public Result diyListPage(@RequestBody QueryPageParam pageParam) {

        HashMap param = pageParam.getParam();
        String name = (String) param.get("name");
        String sex = (String) param.get("sex");
        String roleId = (String) param.get("roleId");

        Page<User> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(name)){
            wrapper.like(User::getName, name);
        }
        if (StringUtils.isNotBlank(sex)){
            wrapper.eq(User::getSex, sex);
        }
        if (StringUtils.isNotBlank(roleId)){
            wrapper.eq(User::getRoleId, roleId);
        }

        IPage result = userService.pageDiy(page, wrapper);
        System.err.println(result.getTotal());

        return Result.suc(result.getRecords(),result.getTotal());

    }

    /*账号唯一验证*/
    @GetMapping("/findByNo")
    public Result findByNo(@RequestParam String no){
        List<User> list = userService.lambdaQuery().eq(User::getNo, no).list();
        return list.size() > 0 ? Result.suc(list):Result.fail();
    }


}

